<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

require_once ('data/api/IDictionary.php');

/**
 * Session management.
 *
 * @author Rodrigo Rutkoski Rodrigues <rutkoski@gmail.com>
 * @package Simplify_Kernel
 */
class Session implements IDictionary
{

  /**
   * Singleton instance of Session.
   *
   * @var Session
   */
  private static $instance;

  /**
   * Get the instance of Session.
   *
   * @return Session
   */
  public static function getInstance()
  {
    if (empty(self::$instance))
      self::$instance = new self();
    return self::$instance;
  }

  /**
   * Constructor.
   *
   * @return void
   */
  private function __construct()
  {
    $this->start(Simplify::getRequest()->get()->get('sid'));

    if (Simplify::getRequest()->get()->has('SESSION_RESET')) {
      session_regenerate_id(true);
      $this->reset();
    }
  }

  /**
   * Start a session.
   *
   * @param string $id Session id.
   * @return void
   */
  public function start($id = null)
  {
    if (! empty($id))
      session_id($id);
    session_start();
  }

  /**
   * Destroy the current session.
   *
   * @return void
   */
  public function destroy()
  {
    $this->reset();

    if (isset($_COOKIE[session_name()]))
      setcookie(session_name(), '', time() - 42000, '/');

    session_destroy();
  }

  public function flash($name, $value = null)
  {
    $data = $this->get('__flash__');
    
    if (empty($data)) {
      $data = array();
    }
    
    if (is_null($value)) {
      $value = sy_get_param($data, $name);
      unset($data[$name]);
    }
    else {
      $data[$name] = $value;
    }
    
    $this->set('__flash__', $data);
    
    return $value;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#copyAll($data, $except, $filter)
   */
  public function copyAll($data)
  {
    $args = func_get_args();
    $except = sy_get_param($args, 1);
    $filter = sy_get_param($args, 2);

    if ($data instanceof IDictionary)
      $data = $data->getAll();

    foreach ($data as $name => $value) {
      if (! empty($except) && in_array($name, ( array ) $except))
        continue;
      if ($filter == Dictionary::FILTER_NULL && is_null($value))
        continue;
      if ($filter == Dictionary::FILTER_EMPTY && empty($value))
        continue;

      $this->set($name, $value);
    }

    return $this;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#del($name)
   */
  public function del($name)
  {
    if (isset($_SESSION[$name])) {
      unset($_SESSION[$name]);
    }
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#get($name, $default, $filter)
   */
  public function get($name)
  {
    $args = func_get_args();
    $default = isset($args[1]) ? $args[1] : null;//sy_get_param($args, 1);
    $filter = isset($args[2]) ? $args[2] : null;//sy_get_param($args, 2);

    if (! $this->has($name, $filter))
      return $default;
    return $_SESSION[$name];
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#getAll()
   */
  public function getAll()
  {
    return $_SESSION;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#getNames()
   */
  public function getNames()
  {
    return array_keys($_SESSION);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#has($name, $filter)
   */
  public function has($name)
  {
    $args = func_get_args();
    $filter = sy_get_param($args, 1);

    if (! isset($_SESSION[$name]))
      return false;
    if ($filter == Dictionary::FILTER_NULL && is_null($_SESSION[$name]))
      return false;
    if ($filter == Dictionary::FILTER_EMPTY && empty($_SESSION[$name]))
      return false;
    return true;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#reset($data, $except, $filter)
   */
  public function reset($data = null)
  {
    $args = func_get_args();
    $except = sy_get_param($args, 1);
    $filter = sy_get_param($args, 2);

    $_SESSION = array();
    if (! empty($data))
      $this->copyAll($data, $except, $filter);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#set($name, $value)
   */
  public function set($name, $value)
  {
    $_SESSION[$name] = $value;
  }

}

?>